/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BACustomTabBarBackground.m
//  redia-bapps-ios
//
//  Created by Mads Darø Kristensen on 10/2/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BACustomTabBarBackground.h"

@implementation BACustomTabBarBackground

- (id)initWithFrame:(CGRect)frame items:(NSInteger)items
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _items = items;
        self.clipsToBounds = YES;
        
        UIColor* linecolor = [UIColor colorWithWhite:224.0/255.0 alpha:1];
        CGRect mybounds = self.bounds;
        CGFloat xIncrement = mybounds.size.width / _items;
        
        if (RD_IS_IPAD) {
            xIncrement = 110; //NB: this is a constant that Apple has defined, which is hidden in earlier versions than iOS     7.0
            CGFloat xOffset = (self.frame.size.width/2.0) - ((_items*xIncrement)/2.0);
            
            for (int i=0; i <= _items; i++) {
                UIView* newview = [[UIView alloc] initWithFrame:CGRectMake(xOffset + xIncrement*i, 0, 1, mybounds.size.height)];
                newview.backgroundColor = linecolor;
                [self addSubview:newview];
            }
        } else {
            for (int i=1; i < _items; i++) {
                UIView* newview = [[UIView alloc] initWithFrame:CGRectMake(xIncrement*i, 0, 1, mybounds.size.height)];
                newview.backgroundColor = linecolor;
                [self addSubview:newview];
            }
        }
    }
    return self;
}

-(void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    if (_items>0) {
        CGFloat xIncrement = frame.size.width / _items;
        if (RD_IS_IPAD) {
            int i = 0;
            xIncrement = 110; //NB: this is a constant that Apple has defined, which is hidden in earlier versions than iOS     7.0
            CGRect mybounds = self.bounds;
            CGFloat xOffset = (self.frame.size.width/2.0) - ((_items*xIncrement)/2.0);
            
            for (UIView* sub in self.subviews) {
                sub.frame = CGRectMake(xOffset + xIncrement*i, 0, 1, mybounds.size.height);
                i++;
            }
        } else {
            int i = 1;
            for (UIView* sub in self.subviews) {
                sub.frame = CGRectMake(xIncrement*i, 0, 1, frame.size.height);
                i++;
            }
        }
        
    }

}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
/* doesn't work properly
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGFloat grey[4] = {224.0/255.0, 224.0/255.0, 224.0/255.0, 1.0};
    CGContextSetStrokeColor(context, grey);
    CGFloat spacing = self.bounds.size.width / self.items;
    for (int i = 1; i < self.items; i++) {
        CGFloat xCoordinate = i * spacing;
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, xCoordinate, 0);
        CGContextAddLineToPoint(context, xCoordinate, self.bounds.size.height);
        CGContextStrokePath(context);
    }
}
*/

@end
